using System.Threading.Tasks;
using XDQ.VAT.Permissions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using XDQ.VAT.Localization;
using XDQ.VAT.MultiTenancy;
using Volo.Abp.TenantManagement.Web.Navigation;
using Volo.Abp.UI.Navigation;

namespace XDQ.VAT.Web.Menus
{
    public class VATMenuContributor : IMenuContributor
    {
        public async Task ConfigureMenuAsync(MenuConfigurationContext context)
        {
            if (context.Menu.Name == StandardMenus.Main)
            {
                await ConfigureMainMenuAsync(context);
            }
        }

        private async Task ConfigureMainMenuAsync(MenuConfigurationContext context)
        {
            if (!MultiTenancyConsts.IsEnabled)
            {
                var administration = context.Menu.GetAdministration();
                administration.TryRemoveMenuItem(TenantManagementMenuNames.GroupName);
            }

            var l = context.GetLocalizer<VATResource>();

            context.Menu.Items.Insert(0, new ApplicationMenuItem(VATMenus.Home, l["Menu:Home"], "~/"));

            if (await context.IsGrantedAsync(VATPermissions.AppStudent.Default))
            {
                context.Menu.AddItem(
                    new ApplicationMenuItem(VATMenus.AppStudent, l["Menu:AppStudent"], "/Students/AppStudent")
                );
            }
            if (await context.IsGrantedAsync(VATPermissions.AppClass.Default))
            {
                context.Menu.AddItem(
                    new ApplicationMenuItem(VATMenus.AppClass, l["Menu:AppClass"], "/Classes/AppClass")
                );
            }
            if (await context.IsGrantedAsync(VATPermissions.AppCourse.Default))
            {
                context.Menu.AddItem(
                    new ApplicationMenuItem(VATMenus.AppCourse, l["Menu:AppCourse"], "/Courses/AppCourse")
                );
            }
            if (await context.IsGrantedAsync(VATPermissions.AppExam.Default))
            {
                context.Menu.AddItem(
                    new ApplicationMenuItem(VATMenus.AppExam, l["Menu:AppExam"], "/Exams/AppExam")
                );
            }
        }
    }
}
